﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;

namespace QuanLyDatDai
{
    public partial class FrmDonHopTachThua : Form
    {
        public FrmDonHopTachThua()
        {
            InitializeComponent();
        }

        private void FrmGopThua_Load(object sender, EventArgs e)
        {
            
        }

        private void radioButtonHopThua_CheckedChanged(object sender, EventArgs e)
        {
            groupBoxGopThua.Visible = true;
            groupBoxTachThua.Visible = false;
        }

        private void radioButtonTachThua_CheckedChanged(object sender, EventArgs e)
        {
            groupBoxTachThua.Visible = true;
            groupBoxGopThua.Visible = false;            
            comboBoxTachThua.SelectedIndex=0;
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            ThuaDatBLL thuadat = new ThuaDatBLL();
            List<TIMTHUADATTHEOCHUSUDUNG_Result> td = thuadat.TimThuaDatTheoChuSuDung(txtTenChuSoHuu.Text,txtSoCmnd.Text);
            ListViewItem item;
            listViewTimKiemChuSuDung.Items.Clear();
            for(int i=0;i<td.Count;i++)
            {               
                item = listViewTimKiemChuSuDung.Items.Add(td[i].MACSD.Trim());
                item.SubItems.Add(td[i].TENCSD);
                item.SubItems.Add(td[i].MATHUADAT.Trim());
                item.SubItems.Add(td[i].MATOBANDO);
                item.SubItems.Add(td[i].DIACHI);
                item.SubItems.Add(td[i].MAGCN);
            }
        }

        private bool KiemTraItemTonTaiListView()
        {   
            bool check = false;
            foreach (ListViewItem item in listViewDanhSachTD.Items)
            {
                if (item.Text.Trim() == listViewTimKiemChuSuDung.SelectedItems[0].SubItems[2].Text.Trim())
                {
                    check = true;
                    break;
                }
            }
            return check;
        }

        private void btnTachThuaLuu_Click(object sender, EventArgs e)
        {
            DonHopTachThuaBLL don = new DonHopTachThuaBLL();
            try
            {
                don.LuuDonXinHopTachThua(txtMaSoDon.Text, dateTimePickerNgayLamDon.Value.Date, "1", lblMaCSD.Text, txtLyDoTach.Text);
                MessageBox.Show("Lưu đơn thành công", "Thông báo");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.InnerException.Message, "Lỗi");
            }
        }

        private void btnHopThuadLuu_Click(object sender, EventArgs e)
        {
            DonHopTachThuaBLL don = new DonHopTachThuaBLL();
            try
            {
                don.LuuDonXinHopTachThua(txtMaSoDon.Text, dateTimePickerNgayLamDon.Value, "0", lblMaCSD.Text, txtLyDo.Text);
                MessageBox.Show("Lưu đơn thành công", "Thông báo");
                btnCapNhatThuaCu.Visible = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.InnerException.Message, "Lỗi");
            }
        }

        private void btnDuaVoDanhSach_Click(object sender, EventArgs e)
        {
            ThuaDatBLL thuadat = new ThuaDatBLL();
            ListViewItem item;

            if (radioButtonHopThua.Checked == true)
            {
                if (listViewTimKiemChuSuDung.SelectedItems.Count == 0)
                    return;
                else
                {

                    int i = listViewTimKiemChuSuDung.FocusedItem.Index;
                    lblMaCSD.Text = listViewTimKiemChuSuDung.Items[i].Text;
                    if (!KiemTraItemTonTaiListView())
                    {                        
                        item = listViewDanhSachTD.Items.Add(listViewTimKiemChuSuDung.Items[i].SubItems[2].Text.Trim());
                        item.SubItems.Add(listViewTimKiemChuSuDung.Items[i].SubItems[3].Text);
                        item.SubItems.Add(listViewTimKiemChuSuDung.Items[i].SubItems[4].Text);
                        item.SubItems.Add(listViewTimKiemChuSuDung.Items[i].SubItems[5].Text.Trim());
                    }
                    else
                    {
                        MessageBox.Show("Item này đã tồn tại rồi!", "Thông báo");
                    }
                }
            }
            else
            {
                if (listViewTimKiemChuSuDung.SelectedItems.Count == 0)
                    return;
                listViewTachThua.Items.Clear();
                int i = listViewTimKiemChuSuDung.FocusedItem.Index;
                item = listViewTachThua.Items.Add(listViewTimKiemChuSuDung.Items[i].SubItems[2].Text.Trim());
                item.SubItems.Add(listViewTimKiemChuSuDung.Items[i].SubItems[3].Text);
                item.SubItems.Add(listViewTimKiemChuSuDung.Items[i].SubItems[4].Text);
                item.SubItems.Add(listViewTimKiemChuSuDung.Items[i].SubItems[5].Text.Trim());
            }
        }

        private void btnCapNhatThuaCu_Click(object sender, EventArgs e)
        {
            FrmGopThua thuadat = new FrmGopThua();
            thuadat_chusudung.Clear();
            DanhSachCacThuaCanGop();
            thuadat.ThuaDat_ChuSuDung = thuadat_chusudung; 
            thuadat.Visible = true;
        }
        private List<TIMTHUADATTHEOCHUSUDUNG_Result> thuadat_chusudung= new List<TIMTHUADATTHEOCHUSUDUNG_Result>();

        private void btnLayKhoiDS_Click(object sender, EventArgs e)
        {
            if (listViewDanhSachTD.SelectedItems.Count == 0)
                return;
            else 
            {
                int i = listViewDanhSachTD.FocusedItem.Index;
                listViewDanhSachTD.Items.Remove(listViewDanhSachTD.Items[i]);
            }
        }

        private void btnXemThuaDat_Click(object sender, EventArgs e)
        {
            FrmDaGiac dagiac = new FrmDaGiac();
            dagiac.MangThuaDat = DanhSachCacThuaDatCanGopDeHienThi();
            dagiac.Visible = true;
            
        }

        private string[] DanhSachCacThuaDatCanGopDeHienThi()
        {
            string[] maThuaDat = new string[listViewDanhSachTD.Items.Count];
            for (int i = 0; i < listViewDanhSachTD.Items.Count; i++)
            {
                maThuaDat[i] = new DaGiacBLL().LayMaDaGiacTuMaThuaDat(listViewDanhSachTD.Items[i].Text);
            }

            return maThuaDat;
        }

        private void DanhSachCacThuaCanGop()
        {
            for (int i = 0; i < listViewDanhSachTD.Items.Count; i++)
            {
                TIMTHUADATTHEOCHUSUDUNG_Result td_csh = new TIMTHUADATTHEOCHUSUDUNG_Result();
                td_csh.MATHUADAT = listViewDanhSachTD.Items[i].Text.Trim();
                td_csh.DIACHI = listViewDanhSachTD.Items[i].SubItems[1].Text;
                td_csh.TENCSD = listViewDanhSachTD.Items[i].SubItems[2].Text;
                thuadat_chusudung.Add(td_csh);                
            }                
        }
    }
}
